from rest_framework import serializers

from lhc_questions.serializers import ReplySerializerList
from xj_users.models import User, Label, Question, Enterprise, Article, Reply


class a(serializers.ModelSerializer):
    class Meta:
        model = Article
        fields = ('id', 'title')

# 个人信息表序列化器
class UserSerialzier(serializers.ModelSerializer):
    """
    个人信息表序列化器
    """
    token = serializers.CharField(read_only=True)
    password = serializers.CharField(write_only=True)
    articles = a(read_only=True, many=True)

    class Meta:
        model = User
        fields = ('id', 'username', 'password' ,'mobile', 'avatar', 'token', 'articles')
        # fields = '__all__'
    def create(self, validated_data):
        user = User.objects.create_user(**validated_data)
        from rest_framework_jwt.settings import api_settings
        jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER
        jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER

        payload = jwt_payload_handler(user)
        token = jwt_encode_handler(payload)
        user.token = token
        return user

# 标签序列化器
class LabelsSerialzier(serializers.ModelSerializer):
    class Meta:
        model = Label
        fields = '__all__'

# 问题序列化器
class QuestionSerialzier(serializers.ModelSerializer):
    user = serializers.StringRelatedField(read_only=True)
    labels = LabelsSerialzier(read_only=True,many=True)
    class Meta:
        model = Question
        fields = '__all__'

# 回答的问题序列化器
class ReplySerialzier(serializers.ModelSerializer):
    class Meta:
        model = Reply
        fields = '__all__'

# 文章序列化器
class ArticlesSerialzier(serializers.ModelSerializer):
    user = UserSerialzier(read_only=True)
    collected_users = serializers.PrimaryKeyRelatedField(
        label='谁收藏了该文章',
        read_only=True,
        many=True)
    class Meta:
        model = Article
        fields = '__all__'

# 企业序列化器
class EnterpisesSerialzier(serializers.ModelSerializer):
    recruits = serializers.PrimaryKeyRelatedField(read_only=True,many=True)
    class Meta:
        model = Enterprise
        fields = ['id','name','labels','logo','summary','recruits']

# 个人中心信息表序列化器
class User1Serialzier(serializers.ModelSerializer):
    """
    个人中心信息表序列化器
    """
    # id = serializers.IntegerField(read_only=True)
    # token = serializers.CharField(read_only=True)
    password = serializers.CharField(required=False,write_only=True)
    labels = LabelsSerialzier(read_only=True,many=True) # 使用标签序列化器
    questions = QuestionSerialzier(many=True,read_only=True) # 提的问题
    # replies = ReplySerialzier(many=True) # 回答的问题
    answer_question = ReplySerializerList(read_only=True, many=True)
    collected_articles = ArticlesSerialzier(read_only=True, many=True) # 收集的文章
    enterpises = EnterpisesSerialzier(read_only=True, many=True) # 收藏的企业
    class Meta:
        model = User
        fields = '__all__'

# 修改擅长技术
class UpdateLabelSerializer(serializers.ModelSerializer):
    labels = serializers.PrimaryKeyRelatedField(required=True,
                                                many=True,
                                                queryset=Label.objects.all())
    class Meta:
        model = User
        fields = ('id','labels')

# 修改擅长技术
class UpdateLabelSerializer(serializers.ModelSerializer):
    labels = serializers.PrimaryKeyRelatedField(required=True,
                                                many=True,
                                                queryset=Label.objects.all())
    class Meta:
        model = User
        fields = ('id','labels')